package view;

import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;

import model.Computation;

public class PopulationGraph extends JPanel{
	private Computation computation;
        
	DefaultCategoryDataset dataset;
    JFreeChart lineChart;
    ChartPanel chartPanel;
    
    public PopulationGraph() {
    	dataset = new DefaultCategoryDataset();
    	lineChart = createChart(dataset);
    	chartPanel = new ChartPanel(lineChart);
    	chartPanel.setPreferredSize(new Dimension(600, 400));
    	add(chartPanel);
    	
    }
    
    private DefaultCategoryDataset createDataset(Computation newComputation) {
        final String series1 = "Population";
		
		if (computation != null) {
			for (int i = 0; i < computation.getPopulation().size(); i++) {
	        	dataset.removeValue(series1, computation.getPopulation().get(i).toString());
	        	
	        }
		}  
        
        for(int i = 0; i < newComputation.getPopulation().size(); i++)
        	dataset.addValue(newComputation.getPopulationMeans().get(i), series1, newComputation.getPopulation().get(i).toString());
	        
        this.computation = newComputation;
        
        return dataset;
        }

    private JFreeChart createChart(final CategoryDataset dataset) {
    	final JFreeChart chart = ChartFactory.createLineChart(
	        "Population Simulation",       
	        "x",                    
	        "f(x)",                   
	        dataset,                   
	        PlotOrientation.VERTICAL,  
	        true,                      
	        true,                      
	        false);


		    chart.setBackgroundPaint(Color.white);
		
		    final CategoryPlot plot = (CategoryPlot) chart.getPlot();
		    plot.setBackgroundPaint(Color.lightGray);
		    plot.setRangeGridlinePaint(Color.white);
		
		    // customise the range axis...
		    final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
		    rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
		    rangeAxis.setAutoRangeIncludesZero(true);
		
		    final LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer();


		    renderer.setSeriesStroke(
		        0, new BasicStroke(
		            2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND,
		            1.0f, new float[] {10.0f, 6.0f}, 0.0f
		        )
		    );
		    renderer.setSeriesStroke(
		        1, new BasicStroke(
		            2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND,
		            1.0f, new float[] {6.0f, 6.0f}, 0.0f
		        )
		    );
		    renderer.setSeriesStroke(
		        2, new BasicStroke(
		            2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND,
		            1.0f, new float[] {2.0f, 6.0f}, 0.0f
		        )
		    );
    
		    return chart;

}

    public void updateData(Computation computation){
        //this.computation = computation;
        
        dataset = createDataset(computation);
        lineChart = createChart(dataset);

        chartPanel.setPreferredSize(new Dimension(600, 400));
        chartPanel.setChart(lineChart);
        
        }
  
}